﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
using System.Xml.Linq;
using System.Web;

namespace TireService.Data
{
    public class ScheduledJobs
    {
        public void ExecuteJob(TireServiceDB db, ScheduledJobQueue Job) 
        {
            if ((int)Shared.ScheduledJobId.WebshopOrderInterface == Job.ScheduledJobId)
            {
                // TODO: Do something
            }

            if ((int)Shared.ScheduledJobId.CustomerServiceReminder == Job.ScheduledJobId)
            {
                // TODO: Do something
            }

            if ((int)Shared.ScheduledJobId.BenifyOrderInterface == Job.ScheduledJobId)
            {
                Interfaces api = new Interfaces();

                // Get benify partner
                PartnerListEDI partner = db.GetPartnerEDI(1);
                
                // Create xml filename
                string month = DateTime.Now.Month.ToString();
                if (month.Length == 1)
                {
                    month = "0" + month;
                }

                string day = DateTime.Now.Day.ToString();
                if (day.Length == 1)
                {
                    day = "0" + day;
                }
                string XMLfilename = "Mobilhjulskift_" + DateTime.Now.Year.ToString() + month + day;

                // Copy interface file
                System.UriBuilder UriBenify = new UriBuilder("ftp", partner.ServerLink, Convert.ToInt32(partner.Port), XMLfilename);
                api.CopyOrderXMLfileFromBenify(UriBenify.Uri, partner.Username, partner.Password);
            }

            // Update job with runtime
            Job.RealisedExecutionTime = DateTime.Now;

            // Submit to database
            db.SubmitChanges();
        }

    }
}
